﻿using QldtSdh.DAL.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QldtSdh.GUI.Common
{
    using BLL;
    using DAL;
    using DAL.Models;
    using GUI.Utils;
    using QldtSdh.GUI.Common;
    using QldtSdh.Utils;

    public partial class FormChonHocVien : QldtSdh.GUI.Common.BaseDialog
    {
        private List<string> _ExclusionMaHocVienList = new List<string>();
        private bool _DoNotLoadLopKhoaNganhList = false;
        private bool _DoNotLoadKhoaHoc_NganhDaoTaoList = false;
        private bool _DoNotLoadHocVienList = false;

        private KhoaHoc SelectedKhoaHoc
        {
            get
            {
                KhoaHoc khoaHoc = null;
                if (bsKhoaHoc.Current != null)
                    khoaHoc = bsKhoaHoc.Current as KhoaHoc;
                return khoaHoc;
            }
        }

        private KhoaHoc_NganhDaoTao SelectedKhoaHoc_NganhDaoTao
        {
            get
            {
                KhoaHoc_NganhDaoTao nganh = null;
                if (bsNganhDaoTao.Current != null)
                    nganh = bsNganhDaoTao.Current as KhoaHoc_NganhDaoTao;
                return nganh;
            }
        }

        private CoSoDaoTao SelectedCoSoDaoTao
        {
            get
            {
                CoSoDaoTao coso = null;
                if (bsCoSoDaoTao.Current != null)
                    coso = bsCoSoDaoTao.Current as CoSoDaoTao;
                return coso;
            }
        }

        private LopKhoaNganh SelectedLopKhoaNganh
        {
            get
            {
                LopKhoaNganh lop = null;
                if (bsLopKhoaNganh.Current != null)
                    lop = bsLopKhoaNganh.Current as LopKhoaNganh;
                return lop;
            }
        }

        public HocVien SelectedHocVien
        {
            get
            {
                HocVien hv = null;
                if (bsHocVien.Current != null)
                    hv = bsHocVien.Current as HocVien;
                return hv;
            }
        }

        public FormChonHocVien()
        {
            InitializeComponent();
            gridLopKhoaNganh.SetDataGridViewAsReadonly();
            gridHocVien.SetDataGridViewAsReadonly(rowHeadersVisible: true, columnHeadersVisible: true);
            gridHocVien.AddRowIndexing();

            _DoNotLoadLopKhoaNganhList = true;
            _DoNotLoadKhoaHoc_NganhDaoTaoList = true;
            try
            {
                // Nạp danh sách cơ sở đào tạo
                bsCoSoDaoTao.DataSource = BizCoSoDaoTao.GetListCoSoDaoTao();
                cbxCoSoDaoTao.DisplayMember = "TenCoSoDaoTao";
                cbxCoSoDaoTao.DataSource = bsCoSoDaoTao;

                // Nạp danh sách khoá học
                bsKhoaHoc.DataSource = BizKhoaHoc.GetList();
                cbxKhoaHoc.DisplayMember = "TenKhoaHoc";
                cbxKhoaHoc.DataSource = bsKhoaHoc;
            }
            finally
            {
                _DoNotLoadLopKhoaNganhList = false;
                _DoNotLoadKhoaHoc_NganhDaoTaoList = false;
            }

            UpdateGuiBySelectedKhoaHoc();
        }

        private void UpdateGuiBySelectedKhoaHoc()
        {
            _DoNotLoadLopKhoaNganhList = true;
            var khoaHoc = this.SelectedKhoaHoc;
            if (khoaHoc == null)
                bsNganhDaoTao.DataSource = null;
            else
                bsNganhDaoTao.DataSource = BizKhoaHoc_NganhDaoTao.GetListByKhoaHoc(khoaHoc.MaKhoaHocGUID);
            cbxNganhDaoTao.DisplayMember = "TenNganhDaoTao";
            cbxNganhDaoTao.DataSource = bsNganhDaoTao;
            _DoNotLoadLopKhoaNganhList = false;

            UpdateGuiBySelectedKhoaHoc_NganhDaoTao();
        }

        private void UpdateGuiBySelectedKhoaHoc_NganhDaoTao()
        {
            NapDanhSachLopKhoaNganh();
        }

        private void UpdateBySelectedCoSoDaoTao()
        {
            NapDanhSachLopKhoaNganh();
        }

        private void NapDanhSachLopKhoaNganh()
        {
            _DoNotLoadHocVienList = true;
            var coSoDaoTao = this.SelectedCoSoDaoTao;
            var khoaHoc_NganhDaoTao = this.SelectedKhoaHoc_NganhDaoTao;

            if (coSoDaoTao == null || khoaHoc_NganhDaoTao == null)
                bsLopKhoaNganh.DataSource = null;
            else
            {
                var lstLopKhoaNganh = BizLopKhoaNganh.GetListByMaDaoTaoKhoaNganh(khoaHoc_NganhDaoTao.MaDaoTaoKhoaNganhGUID, coSoDaoTao.MaCoSoDaoTaoGUID);
                bsLopKhoaNganh.DataSource = lstLopKhoaNganh;
            }

            gridLopKhoaNganh.SetDataSource(bsLopKhoaNganh);
            _DoNotLoadHocVienList = false;

            UpdateGuiBySelectedLopKhoaNganh();
        }

        private void UpdateGuiBySelectedLopKhoaNganh()
        {
            var lopKN = this.SelectedLopKhoaNganh;
            if (lopKN == null)
                bsHocVien.DataSource = null;
            else
                bsHocVien.DataSource = BizHocVien.GetList(lopKN.MaLopKhoaNganhGUID);
            gridHocVien.SetDataSource(bsHocVien);
        }

        private void bsKhoaHoc_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadKhoaHoc_NganhDaoTaoList)
                return;
            UpdateGuiBySelectedKhoaHoc();
        }

        private void bsNganhDaoTao_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadLopKhoaNganhList)
                return;
            UpdateGuiBySelectedKhoaHoc_NganhDaoTao();
        }

        private void bsLopKhoaNganh_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadHocVienList)
                return;
            UpdateGuiBySelectedLopKhoaNganh();
        }

        private void bsCoSoDaoTao_CurrentChanged(object sender, EventArgs e)
        {
            if (_DoNotLoadLopKhoaNganhList)
                return;
            UpdateGuiBySelectedKhoaHoc_NganhDaoTao();
        }

    }
}
